import sun.rmi.runtime.Log;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.logging.Logger;

@WebServlet(name = "HelloGetTest")
public class HelloGetTest extends HttpServlet {
    HashMap<String, String> mCookieMap = new HashMap<>();


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //TODO 解决汉字乱码
        String name = new String(request.getParameter("name").getBytes("iso-8859-1"), "utf-8");

//        String name = new String(request.getParameter("name").getBytes("utf-8"),"utf-8") ; // idea改了编码 适用

        String url = request.getParameter("url");

        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        PrintWriter writer = response.getWriter();


        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (!mCookieMap.containsKey("cookie_test")) {
                mCookieMap.put("cookie_test", "cookie_value");
                Cookie cookieTest = new Cookie("cookie_test", "cookie_value");
                cookieTest.setMaxAge(10000);//单位为秒
                response.addCookie(cookieTest);

                break;
            }
            writer.println("<p>cookieKey:" + cookie.getName() + ",cookieValue:" + cookie.getValue() + "</p>");
        }

        writer.println("<p>你好 HelloGetTest</p>");
        writer.println("<p>获取得网站：" + name + ",url:" + url + "</p>");
    }
}
